package com.kang.confirm;

import com.kang.utils.RabbitMQUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.MessageProperties;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Author: zhangkangkang
 * @Description: 批量消息确认
 * @date 2021/8/3 11:43
 */
public class Confirm2 {
    public static void main(String[] args) throws IOException, InterruptedException, TimeoutException {
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare("tx43", false, false, false, null);
        //开启确认模式
        channel.confirmSelect();
        for (int i = 0; i < 10; i++) {
            channel.basicPublish("", "tx43", MessageProperties.TEXT_PLAIN, "zsf".getBytes());
        }
        //如果有一条消息失败，直接抛出异常，所有数据都不进去
        channel.waitForConfirmsOrDie();
    }
}
